// 演示scala函数

//
import scala.math._
max(2,7)
sqrt(0)

// 自定义函数
def helloScala(name:String) :String = {
  s"Hello scala, this's ${name}."
}
// 调用函数
helloScala("Team")
val simon = helloScala("Simon")
println(simon)

// Unit 无返回值 函数
def procedure(name:String): Unit = {
  s"Hello scala, this's ${name}."
}
val emp = helloScala("emp")

// 默认参数
def testParam(name:String,age:Int,city:String = "Beijing"): Unit = {
  s"Name: ${name}, Age: ${age}, City: ${city}."
}
val p1 = testParam(name="Tome",age=18)
testParam(age=22,name="Ge")

// 变长参数
def sum(args: Int*) :Int = {
  var res = 0
  for (arg <- args) res += arg
  res
}

sum(1,2,3,4,5,6,7)
sum(9,8)
sum(1 to 100 : _*)

// 扩展例子 递归函数
// 1 to 100 :Int* 被当成了Int*，_*这种形式，可以应用与递归函数
def recSum(args :Int*) :Int = {
  if (args.length == 0) {
    0
  } else {
    args.head + recSum(args.tail:_*)
  }
}

recSum(1,2,3,4,5,6,7)
recSum(1 to 100 :_*)

// Lazy值
